package com.rh.bmw4s.ui;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.rh.bmw4s.R;
import com.rh.bmw4s.bean.ChangePhoneNumBean;
import com.rh.bmw4s.bean.YzmBean;
import com.rh.bmw4s.comm.ConfigValue;
import com.rh.bmw4s.comm.Constant;
import com.rh.bmw4s.utills.CheckUtil;
import com.rh.bmw4s.utills.KeyboardUtils;
import com.rh.bmw4s.utills.LogUtils;
import com.rh.bmw4s.utills.SPUtils;
import com.rh.bmw4s.utills.TimeCount;
import com.rh.bmw4s.utills.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 修改手机号
 */
public class ChangePhoneActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.edit_change_newphone)
    EditText editNewphone;
    @BindView(R.id.edit_change_yzm)
    EditText editYzm;
    @BindView(R.id.btn_change_getyzm)
    TextView btnGetyzm;
    @BindView(R.id.btn_change_begin)
    TextView btnCommit;

    private TimeCount timeCount;
    private Gson gson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_phone);
        ButterKnife.bind(this);
        gson = new Gson();
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("修改手机号");
        timeCount = new TimeCount(btnGetyzm, Color.parseColor("#333333"), 60 * 1000, 1000);
    }

    @OnClick({R.id.title_left_one_btn, R.id.btn_change_getyzm, R.id.btn_change_begin})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                finish();
                break;
            case R.id.btn_change_getyzm:
                String strOld = SPUtils.getInstance().getString(ConfigValue.telPhone);
                String strPhone = editNewphone.getText().toString();
                if (TextUtils.equals(strOld, strPhone)) {
                    ToastUtils.toastForShort(ChangePhoneActivity.this, "与原手机号相同");
                    break;
                }
                if (CheckUtil.isMobileNO(strPhone) && strPhone.length() == 11) {
                    timeCount.start();
                    obtainYzm(strPhone);
                } else {
                    ToastUtils.toastForShort(ChangePhoneActivity.this, "手机号码错误");
                }
                break;
            case R.id.btn_change_begin:
                String strYzm = editYzm.getText().toString();
                if (CheckUtil.isNumeric(strYzm) && strYzm.length() == 6) {
                    commitPhone();
                }
                break;
        }
    }

    private void commitPhone() {
        int anInt = SPUtils.getInstance().getInt(ConfigValue.userId);
        final String phoneNum = editNewphone.getText().toString();
        String yzmNmu = editYzm.getText().toString();
        OkGo.<String>post(Constant.CommitPhone)
                .tag(ChangePhoneActivity.this)
                .params("user_id", anInt)
                .params("mobile", phoneNum)
                .params("code", yzmNmu)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        LogUtils.d("eee", response.body().toString());
                        ChangePhoneNumBean bean = gson.fromJson(response.body().toString(), ChangePhoneNumBean.class);
                        ToastUtils.toastForShort(ChangePhoneActivity.this, bean.getMsg());
                        if (bean.getStatus() == Constant.SUCESS) {
                            SPUtils.getInstance().put(ConfigValue.telPhone, phoneNum);
                            KeyboardUtils.hideSoftInput(ChangePhoneActivity.this);
                            ChangePhoneActivity.this.setResult(14);
                            finish();
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtils.toastForShort(ChangePhoneActivity.this, "修改手机号失败");
                    }
                });
    }

    /**
     * 获取验证码
     */
    private void obtainYzm(String strPhone) {
        OkGo.<String>post(Constant.GetYzm)
                .tag(ChangePhoneActivity.this)
                .params("mobile", strPhone)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        YzmBean bean = gson.fromJson(response.body().toString(), YzmBean.class);
                        if (bean.getStatus() == Constant.SUCESS) {
                            ToastUtils.toastForShort(ChangePhoneActivity.this, bean.getMsg());
                        } else {
                            ToastUtils.toastForShort(ChangePhoneActivity.this, "验证码发送失败");
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtils.toastForShort(ChangePhoneActivity.this, "请求验证码失败");
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timeCount.onFinish();
    }
}
